package com.icehand.demo.service.impl;

import com.icehand.demo.model.User;
import com.icehand.demo.service.DemoService;
import com.icehand.redis.annotation.CacheLock;
import com.icehand.redis.annotation.CacheParam;
import com.icehand.redis.bloom.RedisBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author icehand
 */
@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisBloomFilter redisBloomFilter;

    @Override
    public User cacheTemplate() {
        return (User) redisTemplate.opsForValue().get("demo::user");
    }

    @Override
    @Cacheable(key = "'user'", value = "demo")
    public User cacheAnnotation() {
        return new User()
                .setId(1L)
                .setName("demo");
    }

    @Override
    @CacheLock(prefix = "lock:demo",expire = 10)
    public String cacheLock(@CacheParam User user) {
        return "success";
    }

    @Override
    public boolean putBloomFilter(String key) {
        if(redisBloomFilter.isExist("test",key)){
            return false;
        }

        redisBloomFilter.put("test",key);
        return true;
    }

}
